use common::error::MyError;
use common::page::PageResult;
use entity::letter;
use model::game_email::{LetterPage, SendMail};
use crate::manager::letter::LetterManager;
use crate::manager::postal::PostalManager;

pub struct GameEmailService {}

impl GameEmailService {

    pub async fn send_mail(form:SendMail)->Result<bool,MyError>{
        //创建一个latter
        let latter = LetterManager::create_letter(form.charac_no).await?;
        let items = form.items;
        if form.gold > 0 {
            //先创建金币附件
            send_postal(latter.clone(), form.charac_no, 0, 0, 0, form.gold).await?;
        }
        for item in items {
            send_postal(latter.clone(),form.charac_no,item.item_id, item.item_type, item.count,0).await?;
        }
        Ok(true)
    }

    pub async fn mail_page(form:LetterPage)->Result<PageResult<letter::Model>,MyError>{
        Ok(LetterManager::page(form).await?)
    }
    pub async fn remove_letter(id:i32)->Result<bool,MyError>{
        Ok(LetterManager::remove_letter(id).await?)
    }
    pub async fn remove_postal(id:i32)->Result<bool,MyError>{
        Ok(PostalManager::remove_postal(id).await?)
    }
}

pub async fn send_postal(latter:letter::Model, charac_no:i32, item_id:u32, item_type:u32, count:i32, gold:u32)->Result<bool,MyError>{
    Ok(PostalManager::create_postal(latter.clone(), charac_no, item_id, item_type, count, gold).await.is_ok())
}
